﻿// Decompiled with JetBrains decompiler
// Type: Microsoft.InfoCards.RoamingStoreFile
// Assembly: infocard, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
// MVID: 8E14765A-6610-409A-BA36-099A0642905D
// Assembly location: E:\git\ALLIDA\windll\infocard.exe

using Microsoft.InfoCards.Diagnostics;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Xml;

namespace Microsoft.InfoCards
{
  internal sealed class RoamingStoreFile
  {
    private List<InfoCard> m_infocards;

    public RoamingStoreFile()
    {
      this.m_infocards = new List<InfoCard>();
    }

    public IList<InfoCard> Cards
    {
      get
      {
        return (IList<InfoCard>) this.m_infocards;
      }
    }

    public void Clear()
    {
      foreach (InfoCard infocard in this.m_infocards)
        infocard.ClearSensitiveData();
      this.m_infocards.Clear();
    }

    public void WriteTo(string password, XmlWriter writer)
    {
      byte[] buffer = (byte[]) null;
      byte[] salt = (byte[]) null;
      using (MemoryStream memoryStream1 = new MemoryStream())
      {
        using (XmlWriter writer1 = XmlWriter.Create((Stream) memoryStream1, new XmlWriterSettings()
        {
          OmitXmlDeclaration = true,
          CloseOutput = false,
          Encoding = Encoding.UTF8
        }))
        {
          writer1.WriteStartElement("RoamingStore", "http://schemas.xmlsoap.org/ws/2005/05/identity");
          foreach (InfoCard infocard in this.m_infocards)
            infocard.WriteXml(writer1);
          writer1.WriteEndElement();
          writer1.Flush();
        }
        memoryStream1.Flush();
        memoryStream1.Seek(0L, SeekOrigin.Begin);
        buffer = new byte[RoamingStoreFileUtility.CalculateEncryptedSize(Convert.ToInt32(memoryStream1.Length))];
        using (MemoryStream memoryStream2 = new MemoryStream(buffer))
        {
          RoamingStoreFileUtility.Encrypt((Stream) memoryStream1, (Stream) memoryStream2, password, out salt);
          memoryStream2.Flush();
        }
      }
      writer.WriteStartElement("EncryptedStore", "http://schemas.xmlsoap.org/ws/2005/05/identity");
      writer.WriteStartElement("StoreSalt", "http://schemas.xmlsoap.org/ws/2005/05/identity");
      writer.WriteBase64(salt, 0, salt.Length);
      writer.WriteEndElement();
      writer.WriteStartElement("EncryptedData", "http://www.w3.org/2001/04/xmlenc#");
      writer.WriteStartElement("CipherData", "http://www.w3.org/2001/04/xmlenc#");
      writer.WriteStartElement("CipherValue", "http://www.w3.org/2001/04/xmlenc#");
      writer.WriteBase64(buffer, 0, buffer.Length);
      writer.WriteEndElement();
      writer.WriteEndElement();
      writer.WriteEndElement();
      writer.WriteEndElement();
    }

    public void ReadFrom(string password, XmlReader reader)
    {
      if (!reader.IsStartElement("EncryptedStore", "http://schemas.xmlsoap.org/ws/2005/05/identity"))
        throw InfoCardTrace.ThrowHelperError((Exception) new ImportException(SR.GetString("InvalidImportFile")));
      if (!reader.ReadToDescendant("StoreSalt", "http://schemas.xmlsoap.org/ws/2005/05/identity"))
        throw InfoCardTrace.ThrowHelperError((Exception) new ImportException(SR.GetString("InvalidImportFile")));
      byte[] numArray = new byte[RoamingStoreFileUtility.SaltLength];
      reader.ReadElementContentAsBase64(numArray, 0, numArray.Length);
      if (reader.ReadElementContentAsBase64(new byte[1], 0, 1) != 0)
        throw InfoCardTrace.ThrowHelperError((Exception) new ImportException(SR.GetString("InvalidImportFile")));
      if (!reader.IsStartElement("EncryptedData", "http://www.w3.org/2001/04/xmlenc#"))
        throw InfoCardTrace.ThrowHelperError((Exception) new ImportException(SR.GetString("InvalidImportFile")));
      if (!reader.ReadToDescendant("CipherData", "http://www.w3.org/2001/04/xmlenc#"))
        throw InfoCardTrace.ThrowHelperError((Exception) new ImportException(SR.GetString("InvalidImportFile")));
      if (!reader.ReadToDescendant("CipherValue", "http://www.w3.org/2001/04/xmlenc#"))
        throw InfoCardTrace.ThrowHelperError((Exception) new ImportException(SR.GetString("InvalidImportFile")));
      using (MemoryStream memoryStream1 = new MemoryStream(4096))
      {
        byte[] buffer = new byte[1024];
        int count;
        while ((count = reader.ReadElementContentAsBase64(buffer, 0, buffer.Length)) > 0)
          memoryStream1.Write(buffer, 0, count);
        memoryStream1.Flush();
        memoryStream1.Seek(0L, SeekOrigin.Begin);
        using (MemoryStream memoryStream2 = new MemoryStream(RoamingStoreFileUtility.CalculateDecryptedSize(Convert.ToInt32(memoryStream1.Length))))
        {
          RoamingStoreFileUtility.Decrypt((Stream) memoryStream1, (Stream) memoryStream2, password, numArray);
          memoryStream2.Flush();
          memoryStream2.Seek(0L, SeekOrigin.Begin);
          using (XmlReader reader1 = InfoCardSchemas.CreateReader((Stream) memoryStream2, reader.Settings))
          {
            reader1.Read();
            if (!reader1.IsStartElement("RoamingStore", "http://schemas.xmlsoap.org/ws/2005/05/identity"))
              throw InfoCardTrace.ThrowHelperError((Exception) new ImportException(SR.GetString("InvalidImportFile")));
            if (reader1.ReadToDescendant("RoamingInformationCard", "http://schemas.xmlsoap.org/ws/2005/05/identity"))
            {
              do
              {
                InfoCard infoCard = new InfoCard();
                infoCard.ReadXml(reader1);
                this.m_infocards.Add(infoCard);
                reader1.ReadEndElement();
              }
              while (reader1.IsStartElement("RoamingInformationCard", "http://schemas.xmlsoap.org/ws/2005/05/identity"));
            }
            reader1.ReadEndElement();
          }
        }
      }
      reader.ReadEndElement();
      if (reader.IsStartElement("EncryptionProperties", "http://www.w3.org/2001/04/xmlenc#"))
        reader.Skip();
      reader.ReadEndElement();
      reader.ReadEndElement();
    }
  }
}
